/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import classes.Cliente;
import classes.Veiculo;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Luy Lucas
 */
public class Conexao {

    private static Connection con;
    private static PreparedStatement pst;

    public Conexao() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/stellaris", "root", "santana");

            System.out.println("Conectado");
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean addCliente(Cliente cliente) {
        try {
            pst = (PreparedStatement) con.prepareStatement("insert into cliente (nome, endereco, telefone, celular, rg, cpf, email) values (?,?,?,?,?,?,?);");
            pst.setString(1, cliente.getNome());
            pst.setString(2, cliente.getEndereco());
            pst.setString(3, cliente.getTelefone());
            pst.setString(4, cliente.getCelular());
            pst.setString(5, cliente.getRg());
            pst.setString(6, cliente.getCpf());
            pst.setString(7, cliente.getEmail());
            pst.executeUpdate();
            con.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public boolean addVeiculo(Veiculo veiculo) {
        try {
            pst = (PreparedStatement) con.prepareStatement("insert into veiculo (marca, modelo, anofab, anomod, km, motor, placa) values (?,?,?,?,?,?,?);");
            pst.setString(1, veiculo.getMarca());
            pst.setString(2, veiculo.getModelo());
            pst.setString(3, veiculo.getAnofab());
            pst.setString(4, veiculo.getAnomod());
            pst.setInt(5, veiculo.getKm());
            pst.setFloat(6, veiculo.getMotor());
            pst.setString(7, veiculo.getPlaca());
            pst.executeUpdate();
            System.out.println("Veiculo adicionado");
            con.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    
}
